package com.polaris.live.ui.main.activity

import android.os.Bundle
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.polaris.live.R
import com.polaris.live.common.R.color
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.constant.UserRankConst
import com.polaris.live.databinding.ActivityUserRankBinding
import com.polaris.live.translation.R.string
import com.polaris.live.ui.main.fragment.CharmFragment
import com.polaris.live.ui.main.fragment.GameRankFragment
import com.polaris.live.ui.main.fragment.WealthFragment
import com.polaris.live.utils.getIntOrString
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.login.UserRankViewModel

/**
 * 用户榜单
 *
 * @author Created by lucas on 2023/10/16 17:10
 * @since 1.0.0
 */
class UserRankActivity : BaseVbActivity<UserRankViewModel, ActivityUserRankBinding>() {

    private val mInitPage by lazy { intent.getIntOrString("page", 0) }

    private val mRankType by lazy { intent.getIntOrString("rankType", UserRankConst.RANK_DAY) }

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.llTab)
        setStatusBarDarkFont(false)
        setNavigationBarColor(color.color_0f112a)
        setNavigationBarDarkFont(false)
    }

    override fun initView(savedInstanceState: Bundle?) {
        val listFragments = mutableListOf<Fragment>()
        listFragments.add(CharmFragment().apply {
            setOnChange {
                if (it == 0) {
                    this@UserRankActivity.mBinding.root.setBackgroundResource(R.drawable.bg_bangdan)
                } else {
                    this@UserRankActivity.mBinding.root.setBackgroundResource(R.drawable.bg_month_list)
                }
            }
            arguments = bundleOf("rankType" to mRankType)
        })
        listFragments.add(WealthFragment().apply {
            setOnChange {
                if (it == 0) {
                    this@UserRankActivity.mBinding.root.setBackgroundResource(R.drawable.bg_bangdan)
                } else {
                    this@UserRankActivity.mBinding.root.setBackgroundResource(R.drawable.bg_month_list)
                }
            }
            arguments = bundleOf("rankType" to mRankType)
        })
        if (UserManager.isSafeUser()) {
            listFragments.add(GameRankFragment().apply {
                setOnChange {
                    if (it == 0) {
                        this@UserRankActivity.mBinding.root.setBackgroundResource(R.drawable.bg_bangdan)
                    } else {
                        this@UserRankActivity.mBinding.root.setBackgroundResource(R.drawable.bg_month_list)
                    }
                }
                arguments = bundleOf("rankType" to mRankType)
            })
        }

        val adapter = ViewPagerAdapter(listFragments, this)
        mBinding.vp2.adapter = adapter

        val tabText =
            listOf(getString(string.charm), getString(string.wealth), getString(string.game_rank))
        TabLayoutMediator(mBinding.tab, mBinding.vp2) { tab, position ->
            val tabView = TextView(this)
            tabView.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent))
            tabView.text = tabText[position]
            tabView.textSize = 16F
            tab.customView = tabView
        }.attach()
        mBinding.ivBack.setOnSingleClickListener {
            finish()
        }
        mBinding.vp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                for (index in 0 until mBinding.tab.tabCount) {
                    val tabView = mBinding.tab.getTabAt(index)?.customView as TextView
                    if (position == index) {
                        tabView.setTextColor(
                            ContextCompat.getColor(this@UserRankActivity, R.color.white)
                        )
                    } else {
                        tabView.setTextColor(
                            ContextCompat.getColor(this@UserRankActivity, color.color_80ffffff)
                        )
                    }
                }
            }
        })
        if (listFragments.size - 1 >= mInitPage) {
            mBinding.vp2.setCurrentItem(mInitPage, false)
        }
        mBinding.ivRankRule.setOnSingleClickListener {
            Router.builder(RoutePath.RANK_RULE_ACTIVITY).navigation(this)
        }
    }
}